﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Zxb.DayWork.Core;

namespace Zxb.DayWork.Logic
{
    public class UpEstateCaseNum
    {
        public static void Run()
        {
            SqlConnection sqlConn = null;            
            try
            {                
                sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ZxbWriter"].ConnectionString);
                DataTable dt = GetEstateCaseNum();
                SqlCommand cmd = new SqlCommand("UpEstateTotalCaseNum", sqlConn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter tvpParam = cmd.Parameters.AddWithValue("@tvpECN", dt);
                tvpParam.SqlDbType = SqlDbType.Structured;
                tvpParam.TypeName = "UdtEstateCaseNum";
                
                sqlConn.Open();
                if (dt != null && dt.Rows.Count != 0)
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.InfoLog.Error("更新楼盘案例总个数是出错:"+ex.Message);
            }
            finally
            {
                if (sqlConn != null)
                {
                    sqlConn.Close();
                }
            }
        }

        private static DataTable GetEstateCaseNum()
        {
            try
            {
                Database db = DB.TaskReader;
                string selSql = "select RealEstateID, COUNT(CaseID) TotalNewCaseNum from cases with(nolock) group by RealEstateID";
                DbCommand Cmd = db.GetSqlStringCommand(selSql.ToString());
                return db.ExecuteDataSet(Cmd).Tables[0];
            }
            catch (Exception ex)
            { 
                Logger.InfoLog.Error("抽取楼盘案例总个数时出错:"+ex.Message);
                return null;
            }
        }
    }
}
